﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using DbCommon;

namespace Web.admin.news
{
    public partial class list : System.Web.UI.Page
    {
        static DataTable table;
        static int ItemCount;//总条数
        static int PageCount;//总页数
        static int PageIndex;//当前页
        static int PageSize = 10;//分页数据长度
        DbHelper dbhelper = new DbHelper();
        BLL.NewsContent newsContent = new BLL.NewsContent();
        BLL.NewsClass newsClass = new BLL.NewsClass();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["newsClass"] = null; 
                //每次第一次加载的时候就将当前页置为1
                PageIndex = 1;
                ItemCount = dbhelper.GetCount("v_news","NewsIsShow= 'true'");
                //总页数
                PageCount = (ItemCount + PageSize - 1) / PageSize;
                for (int i = 1; i <= PageCount; i++)
                {
                    DDLPager.Items.Add(new ListItem("第" + i.ToString() + "页", i.ToString()));
                }
                DataBind();
                //managePage.ClassTreeBind( null, ddlClass);
                DDLBind("==所有类别==", ddlClass);
            }
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        protected void DataBind()
        {
            //两个if语句用来处理下一页或者上一页的刷新效果
            if (PageIndex > PageCount)
                PageIndex = PageCount;
            else if (PageIndex < 1)
                PageIndex = 1;
            //开始条数
            int StartItem;
            //结束条数
            int EndItem;
            //开始页码所要获得的条数
            StartItem = PageSize * (PageIndex - 1);
            //条件选择是否超过总的条数
            EndItem = ItemCount > StartItem + PageSize ? StartItem + PageSize : ItemCount;
            if (Session["newsClass"]!=null&&Session["newsClass"].ToString()!="")
                table = dbhelper.GetPageTable("v_news", StartItem, EndItem, "NewspubTime desc", " NewsClassId='" + Session["newsClass"] + "' and NewsIsShow= 'true'");
            else
                table = dbhelper.GetPageTable("v_news", StartItem, EndItem, "NewspubTime desc", "NewsIsShow= 'true'");
            BindPage();
        }

        /// <summary>
        /// page控件绑定
        /// </summary>
        protected void BindPage()
        {
            //绑定当前的前台
            //当前总的页数
            lbPageAll.InnerText = ItemCount.ToString();
            lbPager.InnerText = PageCount.ToString();
            lbIndexPage.InnerText = PageIndex.ToString();
            DDLPager.SelectedValue = PageIndex.ToString();
            if (PageIndex <= 1)
            {
                First.Enabled = false;
                Prev.Enabled = false;
            }
            else
            {
                First.Enabled = true;
                Prev.Enabled = true;
            }
            if (PageIndex >= PageCount)
            {
                Next.Enabled = false;
                Last.Enabled = false;
            }
            else
            {
                Next.Enabled = true;
                Last.Enabled = true;
            }
            RP_Bind();
        }
        /// <summary>
        /// 批量删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int j = 0;
            for (int i = 0; i < RpList.Items.Count; i++)
            {
                string id = ((Label)RpList.Items[i].FindControl("lb_id")).Text;
                CheckBox cb = (CheckBox)RpList.Items[i].FindControl("CbChoose");
                if (cb.Checked)
                {
                    if (newsContent.DelData(id) > 0)
                        j++;
                }
            }
            Response.Write("<script>alert('删除成功，共删除" + j + "条数据');window.location.href='List.aspx'; </script>");
            return;
        }
        /// <summary>
        /// ddlClass change事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlClass_Change( object sender, EventArgs e) 
        {
            PageIndex = 1;
            if (!string.IsNullOrEmpty(ddlClass.SelectedValue))
                ItemCount = dbhelper.GetCount("v_news", " NewsClassId='" + ddlClass.SelectedValue + "' and NewsIsShow= 'true'");
            else
                ItemCount = dbhelper.GetCount("v_news", "NewsIsShow= 'true'");
            //开始页码所要获得的条数
            int StartItem = PageSize * (PageIndex - 1);
            //条件选择是否超过总的条数
            int EndItem = ItemCount > StartItem + PageSize ? StartItem + PageSize : ItemCount;
            if (!string.IsNullOrEmpty(ddlClass.SelectedValue))
            {
                Session["newsClass"] = ddlClass.SelectedValue;
                table = dbhelper.GetPageTable("v_news", StartItem, EndItem, "NewspubTime desc", " NewsClassId='" + ddlClass.SelectedValue + "' and NewsIsShow= 'true'");
            }
            else
            {
                Session["newsClass"] = null;
                table = dbhelper.GetPageTable("v_news", StartItem, EndItem, "NewspubTime desc", "NewsIsShow= 'true'");
                
            }
            //总页数
            PageCount = (ItemCount + PageSize - 1) / PageSize;
            DDLPager.Items.Clear();
            for (int i = 1; i <= PageCount; i++)
            {
                DDLPager.Items.Add(new ListItem("第" + i.ToString() + "页", i.ToString()));
            }
            BindPage();
            RP_Bind();

        }
        /// <summary>
        /// 对DropDownList的绑定
        /// </summary>
        /// <param name="firstStr">第一行显示的数据</param>
        /// <param name="ddl">DropDownList控件</param>
        protected void DDLBind(string firstStr, DropDownList ddl)
        {
            if (!string.IsNullOrEmpty(firstStr))
            {
                ddl.Items.Add(new ListItem(firstStr, ""));
            }
            DataTable table = newsClass.GetNewsClass();
            foreach (DataRow dr in table.Rows)
            {
                ddl.Items.Add(new ListItem(dr["NCTitle"].ToString(),dr["NCId"].ToString() ));
            }
        }
        /// <summary>
        /// 页码点击事件
        /// </summary>
        /// <param name="sender">对象</param>
        /// <param name="e">事件</param>
        protected void PageClick(object sender, EventArgs e)
        {
            //当前页码
            int iCurrentPageIndex = PageIndex;
            //页码总数
            int iPageCount = PageCount;
            string sCommandName = (sender as LinkButton).CommandName;
            switch (sCommandName)
            {
                case "First":
                    PageIndex = 1;
                    break;
                case "Last":
                    PageIndex = iPageCount;
                    break;
                case "Prev":
                    PageIndex = iCurrentPageIndex - 1;
                    break;
                case "Next":
                    PageIndex = iCurrentPageIndex + 1;
                    break;
            }
            DataBind();

        }
        /// <summary>
        /// 根据表格得到相应的列表
        /// </summary>
        /// <returns></returns>
        protected void RP_Bind()
        {
            RpList.DataSource = table;
            RpList.DataBind();
        }
        /// <summary>
        /// 页码下拉列表改变事件
        /// </summary>
        /// <param name="sneder">对象</param>
        /// <param name="e">事件</param>
        protected void DDLpager_Change(object sneder, EventArgs e)
        {
            PageIndex = Convert.ToInt32(DDLPager.SelectedValue);
            DataBind();
        }

    }
}